// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],
    cart: [],
    // 总价格
    totalPrice: 0,
    // 总数量
    totalNum: 0,
    //全选
    allCheck: false
  },

  // 获取收货地址
  handleChooseAddress () {
    wx.getSetting({
      success: (result) => {
        const scopeAddress = result.authSetting["scope.address"];
        if (scopeAddress === false) {
          // 当用户以前点击了取消按钮后
          wx.openSetting();
        }
        wx.chooseAddress({
          success: (result1) => {
            console.log(result1)
            wx.setStorageSync("address", result1);
          }
        })
      }
    });
  },
  // 点击复选框
  handleTapChk (e) {
    const goods_id = e.currentTarget.dataset.id;
    const { cart } = this.data;
    // 获取对应的序号
    const index = cart.findIndex(v => v.goods_id === goods_id);
    cart[index].checked = !cart[index].checked;
    this.setData({
      cart
    })
    // 重新给缓冲赋值
    wx.setStorageSync("cart", cart);
    //重新计算
    this.getTotalPrice()
  },
  // 点击全选框
  handleAllTapChk () {
    const { cart } = this.data;
    let { allCheck } = this.data;
    allCheck = !allCheck;
    cart.forEach(v => {
      v.checked = allCheck;
    })
    this.setData({
      allCheck, cart
    })
    wx.setStorageSync("cart", cart);
    this.getTotalPrice();
  },
  // 点击加号
  addNum (e) {
    const item = e.currentTarget.dataset.info;
    const cart = this.data.cart;
    cart.forEach(i => {
      if (i.goods_id === item.goods_id) {
        i.num++;
      }
    })
    this.setData({
      cart: cart
    })
    wx.setStorageSync("cart", cart);
    this.getTotalPrice();
  },
  // 点击减号
  subNum (e) {
    const item = e.currentTarget.dataset.info;
    const cart = this.data.cart;
    const index = cart.findIndex(v => v.goods_id === item.goods_id);
    console.log(cart[index].num)
    if (cart[index].num === 1) {
      wx.showModal({
        title: '提示',
        content: '确定要删除商品吗？',
        success: (res) => {
          if (res.confirm) {
            cart.splice(index, 1);
            this.setData({
              cart
            })
            wx.setStorageSync("cart", cart);
            this.getTotalPrice();
          } else if (res.cancel) {
            console.log("用户点击了取消");
          }
        }
      })
    } else {
      cart[index].num--;
    }

    this.setData({
      cart: cart
    })
    wx.setStorageSync("cart", cart);
    this.getTotalPrice();

  },
  // 计算总价
  getTotalPrice () {
    const cart = wx.getStorageSync("cart");
    let totalPrice = 0;
    let totalNum = 0;
    let allCheck = true;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.goods_price * v.num;
        totalNum += v.num;
      } else {
        allCheck = false;
      }
    })
    allCheck = cart.length != 0 ? allCheck : false;
    this.setData({
      totalPrice,
      totalNum,
      allCheck
    })
  },
  // 结算
  handlePay () {
    const { address } = this.data;
    const { totalNum } = this.data;
    if (!address.userName) {
      wx.showToast({
        title: '你没有填写收货地址',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: true
      });
    } else if (totalNum === 0) {
      wx.showToast({
        title: '你还没有任何商品',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: true
      });
    } else {
      wx.navigateTo({
        url: '/pages/pay/index'
      });

    }


  },
  onShow: function () {
    const cart = wx.getStorageSync("cart") || [];
    const address = wx.getStorageSync("address")
    this.setData({
      address,
      cart
    })
    this.getTotalPrice();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})